﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Webdiyer.WebControls.Mvc;
using System.Web.Routing;

namespace System
{
	public static class HtmlExtentions
	{
		public static MvcHtmlString NewPager<T>(this HtmlHelper helper, PagedList<T> pagedList, object routeValues = null)
		{
			return helper.Pager(pagedList, new PagerOptions
			{
				PageIndexParameterName = "page",
				FirstPageText = "首页",
				LastPageText = "尾页",
				NextPageText = "下页",
				PrevPageText = "上页"
			}, null, routeValues);
		}

		public static MvcHtmlString ifnotnull<T>(this HtmlHelper helper, T model, Func<T, object> property, string ifNull = null)
		   where T : class
		{
			string strVal = ifNull ?? string.Empty;
			if (model != null)
			{
				var value = property(model);
				if (value != null)
				{
					strVal = value.ToString();
				}
			}

			return MvcHtmlString.Create(strVal);
		}
	}
}
namespace BPM.Web
{
	public class ViewPage : System.Web.Mvc.ViewPage
	{
		public string url(string contentPath)
		{
			return Url.Content(contentPath);
		}
	}

	public class ViewPage<TModel> : System.Web.Mvc.ViewPage<TModel>
	{
		public string url(string contentPath)
		{
			return Url.Content(contentPath);
		}
	}

	public class ViewMasterPage : System.Web.Mvc.ViewMasterPage
	{
		public string url(string contentPath)
		{
			return Url.Content(contentPath);
		}

		public string pageJS()
		{
			var jsPath = "~/Views/" + this.Page.RouteData.GetControllerName() + "/" + this.Page.RouteData.GetActionName() + ".js";

			var physicalPath = Server.MapPath(jsPath);
			if (File.Exists(physicalPath))
			{
				return string.Format("<script src=\"{0}\" type=\"text/javascript\"></script>", url(jsPath));
			}

			return null;
		}
	}
}